import Mock from "mockjs";
import userMock from './user.json'
import userManagementMock from './userManagement.json'
import { GET_TOKEN } from "@/utils/token";

//登录
Mock.mock("/api/user/login", "post", (req) => {
  let { username, password } = JSON.parse(req.body);
  const checkUser = userMock.find(
    (item) => item.username === username && item.password === password
  );
  if (!checkUser) {
    return { code: 201, data: { message: "账号密码不正确" } };
  }
  const token = checkUser.token;
  return { code: 200, data: { message: "login success", token } };
});

//获取用户信息
Mock.mock("/api/user/Info", "get", (options) => {
  const token = GET_TOKEN();
  const checkUser = userMock.find((item) => item.token === token);
  if (!checkUser) {
    return { code: 201, data: { message: "获取用户信息是吧" } };
  }
  return { code: 200, data: { checkUser } };
});

//用户管理信息
Mock.mock("/api/user/management", "post", (req) => {
  let { currentPage, pageSize, userId, userName, userEmail } = JSON.parse(req.body)
  console.log(JSON.parse(req.body));
  let userMsgMock = []
  let findUser = []
  findUser = userManagementMock.filter(user => user.userId.includes(userId) && user.userName.includes(userName) && user.email.includes(userEmail));
  for (let i = currentPage * pageSize - 10; i <= currentPage * pageSize - 1; i++) {
    userMsgMock.push(findUser[i])
  }

  return {
    code: 200,
    data: {
      total: findUser.length,
      userMsgMock: {
        ...userMsgMock
      }

    }
  }
})

//添加用户
Mock.mock('/api/user/addUser', 'post', (req) => {
  let { userId, userName, userEmail, sex, telephone } = JSON.parse(req.body)
  let newUser = {
    userId,
    userName,
    email: userEmail,
    sex,
    telephone
  }
  userManagementMock.push(newUser)
  return {
    code:200,
    data:{
      userManagementMock
    }
  }
})